﻿using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;

namespace Lin.Library.Extensions
{
    /// <summary>
    /// String扩长类
    /// </summary>
    public static class StringExt
    {
        #region 静态属性

        /// <summary>
        /// 生成32GUID
        /// </summary>
        public static String NewGuid { get { return Guid.NewGuid().ToString().Replace("-", "").ToUpper(); } }

        #endregion

        #region 静态方法

        /// <summary>
        /// 返回字符串真实长度, 1个汉字长度为2
        /// </summary>
        public static Int32 Length(string source)
        {
            if (string.IsNullOrEmpty(source))
            {
                return 0;
            }
            return Encoding.Default.GetBytes(source).Length;
        }

        /// <summary>
        /// 是否为ip
        /// </summary>
        public static Boolean IsIP(string source)
        {
            if (string.IsNullOrEmpty(source)) { return false; }

            return Regex.IsMatch(source, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");
        }

        /// <summary>
        /// 是否为日期
        /// </summary>
        public static Boolean IsDate(string source)
        {
            if (string.IsNullOrEmpty(source)) { return false; }

            var date = DateTime.Now;

            return DateTime.TryParse(source, out date);
        }

        /// <summary>
        /// 检测是否为HTTP开头的URL
        /// </summary>
        public static Boolean IsHttpUrl(string source)
        {

            return source.IndexOf("http://") != -1 || source.IndexOf("https://") != -1;
        }

        /// <summary>
        /// 检测是否符合email格式
        /// </summary>
        public static Boolean IsEmail(string source)
        {
            return Regex.IsMatch(source, @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
        }

        /// <summary>
        /// 检测是否有Sql危险字符
        /// </summary>
        public static Boolean IsSafeSqlString(string source)
        {

            return !Regex.IsMatch(source, @"[-|;|,|\/|\(|\)|\[|\]|\}|\{|%|@|\*|!|\']");
        }

        /// <summary>
        /// 长字符串转短字符串,超出部分用...表示
        /// </summary>
        public static String ShortStr(string source, int maxlength)
        {
            if (String.IsNullOrEmpty(source))
            {
                return String.Empty;
            }

            if (source.Length > maxlength)
            {
                return source.Substring(0, maxlength) + "...";
            }

            return source;

        }

        /// <summary>
        /// Text To HTML.
        /// </summary>
        public static String TextToHtml(string source)
        {
            StringBuilder sb = new StringBuilder(source);

            sb.Replace("&", "&amp;");
            sb.Replace("<", "&lt;");
            sb.Replace(">", "&gt;");
            sb.Replace("\"", "&quot;");
            sb.Replace(" ", "&nbsp;");
            sb.Replace("\t", "&nbsp;&nbsp;");
            sb.Replace("\r", "");
            sb.Replace("\n", "<br />");

            return sb.ToString();
        }

        /// <summary>
        /// Html to Text
        /// </summary>
        public static String HtmlToText(string source)
        {
            StringBuilder sb = new StringBuilder(source);

            sb.Replace("<br />", "\n");
            sb.Replace("<br/>", "\n");
            sb.Replace("&nbsp;&nbsp;", "\t");
            sb.Replace("&nbsp;", " ");
            sb.Replace("&#39;", "\'");
            sb.Replace("&quot;", "\"");
            sb.Replace("&gt;", ">");
            sb.Replace("&lt;", "<");
            sb.Replace("&amp;", "&");

            return sb.ToString();
        }

        /// <summary>
        /// 移除Html标记
        /// </summary>
        public static String RemoveHtml(string source)
        {
            string regexstr = @"<[^>]*>";
            return Regex.Replace(source, regexstr, string.Empty, RegexOptions.IgnoreCase).Trim();
        }

        /// <summary>
        /// 返回 URL 字符串的编码结果
        /// </summary>
        public static String UrlEncode(string source)
        {
            return HttpUtility.UrlEncode(source);
        }

        /// <summary>
        /// 返回 URL 字符串的编码结果
        /// </summary>
        public static String UrlDecode(string source)
        {
            return HttpUtility.UrlDecode(source);
        }

        /// <summary>
        /// 将时间换成中文
        /// </summary>
        public static String DateToChineseString(DateTime datetime)
        {
            TimeSpan ts = DateTime.Now - datetime;

            if ((int)ts.TotalDays >= 365)
            {
                return (int)ts.TotalDays / 365 + "年前";
            }
            if ((int)ts.TotalDays >= 30 && ts.TotalDays <= 365)
            {
                return (int)ts.TotalDays / 30 + "月前";
            }
            if ((int)ts.TotalDays == 1)
            {
                return "昨天";
            }
            if ((int)ts.TotalDays == 2)
            {
                return "前天";
            }
            if ((int)ts.TotalDays >= 3 && ts.TotalDays <= 30)
            {
                return (int)ts.TotalDays + "天前";
            }
            if ((int)ts.TotalDays == 0)
            {
                if ((int)ts.TotalHours != 0)
                {
                    return (int)ts.TotalHours + "小时前";
                }
                else
                {
                    if ((int)ts.TotalMinutes == 0)
                    {
                        return "1分钟前";
                    }
                    else
                    {
                        return (int)ts.TotalMinutes + "分钟前";
                    }
                }
            }
            return datetime.ToString("yyyy年MM月dd日 HH:mm");
        }

        #endregion

        #region 扩展方法

        /// <summary>
        /// Object To String 
        /// </summary>
        public static String ToStr(this object source)
        {
            if (source == null)
            {
                return "";
            }

            return source.ToString();
        }

        public static Boolean ToBoolean(this object source)
        {
            if (source == null)
            {
                return false;
            }
            if (source.ToString() == "1" || source.ToString().ToLower() == "true")
            {
                return true;
            }
            return false;
        }

        public static String ToShortStr(this string source, int maxlength)
        {
            return ShortStr(source, maxlength);
        }

        public static DateTime ToDate(this string source)
        {
            DateTime date = new DateTime(1900, 01, 01, 01, 01, 01);

            if (string.IsNullOrEmpty(source))
            {
                return date;
            }

            DateTime.TryParse(source, out date);

            return date;
        }

        #endregion
    }
}
